
package com.kugou.game.sdk.protocol;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.hotfix.PatchInfo;
import com.kugou.game.sdk.utils.ProtocolCmd;

/**
 * 描述:获取补丁信息
 *
 * @author wcj
 * @since 2016年3月11日 上午10:01:52
 */
public class RequestPatchInfoTask {

    private ArrayList<String> mCmdList = new ArrayList<String>();

    public PatchInfoResponse request() {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();

        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        PatchInfoResponse respObj = new PatchInfoResponse();
        ResponsePackage resp = new PatchInfoResponsePackage();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_GET_PATCH_INFO);
        requestParams.add(params);
        mCmdList.add(ProtocolCmd.CMD_GET_PATCH_INFO);

        req.setRequestParams(requestParams);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (Exception e) {
        }
        return null;
    }

    class PatchInfoResponsePackage extends HttpBaseResponsePackage<PatchInfoResponse> {

        @Override
        public void handleResponse(PatchInfoResponse response, String jsonStr) {
            try {
                LogUtil.d("response", "RequestPatchInfoTask = " + jsonStr);

                JSONArray responseArray = new JSONArray(jsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.getJSONObject(i);

                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.getString("code");
                    String message = jsonResp.getString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);

                    if (response.isUseful() && cmd.equals(ProtocolCmd.CMD_GET_PATCH_INFO)) {
                        JSONObject data = jsonResp.optJSONObject("data");
                        PatchInfo info = new PatchInfo();

                        info.setSdkVersionCode(data.optInt("SDKVersionCode"));
                        info.setPatchId(data.optInt("patchId"));
                        info.setPatchMd5(data.optString("patchSign"));
                        info.setPatchUrl(data.optString("patchUrl"));

                        response.setPatchInfo(info);
                        response.setOk(true);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 响应消息
     */
    public static class PatchInfoResponse extends ResponseMessage {
        private PatchInfo patchInfo;

        public PatchInfo getPatchInfo() {
            return patchInfo;
        }

        public void setPatchInfo(PatchInfo patchInfo) {
            this.patchInfo = patchInfo;
        }

    }
}
